import axios from 'axios'
import Qs from 'qs'
import Vue from 'vue'
import router from '@/router'
import store from '@/store'
import {
  getToken,
  removeToken
} from '@/utils/auth'

// request interceptor
axios.interceptors.request.use(config => {
  // Do something before request is sent
  if (store.getters.token) {
    config.headers['accessToken'] = getToken()
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone interceptor
axios.interceptors.response.use(
  response => {
    console.log(response)
    return response.data
  },
  error => {
    
    const blb = new Blob([error], {
      type: 'text/plain'
    })
    const reader = new FileReader()
    console.log(blb, 333, reader, error.response)
    if (error.response) {
      switch (error.response.status) {
        case 900:
          Vue.prototype.$alert(error.response.data.message, {
            type: 'warning'
          })
          break
        case 401:
          console.log(408)
          console.log(error.response)
          Vue.prototype.$alert(error.response.data.message, {
            type: 'error',
            showClose: false
          }).then(() => {
            removeToken()
            router.replace('/login')
          })
          break
        case 408:
          console.log(408)
          console.log(error.response)
          Vue.prototype.$notify({
            title: '没有权限',
            type: 'error',
            duration: 3000
          })
          break
        case 402:
          Vue.prototype.$alert('用户尚未登陆或者session失效', '系统提示', {
            type: 'error',
            showClose: false
          }).then(() => {
            router.replace('/')
          })
          break
        case 501:
          Vue.prototype.$msgbox({
            type: 'warning',
            message: error.response.data,
            title: '系统提示'
          })
          break
        default:
          Vue.prototype.$alert('系统错误', {
            type: 'error'
          })
          break
      }
    }
    return Promise.reject(error)
  })

export {
  axios
}
export default (url = '', data = {}, type = 'POST', responseType) => {
  const option = {
    baseURL: process.env.NODE_ENV === 'development' ? '' : window.APIPATH,
    timeout: 60 * 2 * 1000,
    method: type,
    url: '/api1' + url
  }
  if (responseType === 'blob') {
    option.responseType = 'blob'
  } else {
    option.responseType = responseType
  }

  // 对所有接口注入siteId
  if (data.siteId === undefined) {
    data.siteId = store.getters.siteId
  }

  if (/^post|put|patch$/i.test(type)) {
    option.data = Qs.stringify(data)
  } else if (/^json$/i.test(type)) {
    option.method = 'POST'
    option.data = data
  } else if (/^upload$/i.test(type)) {
    option.method = 'POST'
    var form = new FormData()
    for (const attr in data) {
      form.append(attr, data[attr])
    }
    option.data = form
  } else {
    console.log(option, axios.defaults)
    option.params = data
  }
  return axios(option)
}
